package x.y.z;

import java.util.Arrays;

public class Interview_1_2_CheckPermutation {
    public boolean CheckPermutation(String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        return sort(s1).equals(sort(s2));
    }

    private String sort(String s) {
        char[] chs = s.toCharArray();
        Arrays.sort(chs);
        return new String(chs);
    }

    public boolean CheckPermutation_1(String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        int[] letters = new int[128];
        char[] chs = s1.toCharArray();
        for (char ch : chs) {
            letters[ch]++;
        }
        for (int i = 0; i < s2.length(); i++) {
            int ch = s2.charAt(i);
            letters[ch]--;
            if (letters[ch] < 0) {
                return false;
            }
        }
        return true;
    }
}